package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                Date orderDate = orderSetting.getOrderDate();
                long count = orderSettingDao.findCountByDate(orderDate);//判断数据库中是否存在相同日期

                if (count > 0) {
                    orderSettingDao.editNumByDate(orderSetting);
                } else {
                    orderSettingDao.add(orderSetting);
                }
            }

        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";
        String end = date + "-31";
        Map<String, Object> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> list = new ArrayList<>();
        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                Map<String, Object> m = new HashMap<>();
                m.put("date",orderSetting.getOrderDate().getDate());
                m.put("number",orderSetting.getNumber());
                m.put("reservations",orderSetting.getReservations());
                list.add(m);
            }
        }

        return list;
    }

    @Override
    public void editOrderSettingByDate(OrderSetting orderSetting) {
        long countByDate = orderSettingDao.findCountByDate(orderSetting.getOrderDate());
        if (countByDate>0){
            orderSettingDao.editNumByDate(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }
    }
}
